﻿Public Class clsInhTrDrut
    Inherits clsTrDrut

#Region "Attributes"

    Private mvarPracownik As clsPracownik
    Private mvarPakowanie As clsPakowanie
    Private mvarMaszyna As clsMaszyna
    Private mvarOpcja As clsTrOpcje

    Private mvarRodzic As Collection(Of clsTrRodzic)
    Private mvarRodzicDel As Collection(Of clsTrRodzic)

    Private mvarKontrola As clsInhTrKontrola
    Private mvarKontrolaDel As clsInhTrKontrola

    Private mvarId As Integer
    Private mvarWaga As Double

    Private mvarPozycjaSchematu As clsInhTrPozycjeSchematu

#End Region

#Region "Properties"

    Public Property Pracownik() As clsPracownik
        Get
            Return mvarPracownik
        End Get
        Set(ByVal value As clsPracownik)
            mvarPracownik = value
        End Set
    End Property

    Public Property Pakowanie() As clsPakowanie
        Get
            Return mvarPakowanie
        End Get
        Set(ByVal value As clsPakowanie)
            mvarPakowanie = value
        End Set
    End Property

    Public Property Maszyna() As clsMaszyna
        Get
            Return mvarMaszyna
        End Get
        Set(ByVal value As clsMaszyna)
            mvarMaszyna = value
        End Set
    End Property

    Public Property Opcja() As clsTrOpcje
        Get
            Return mvarOpcja
        End Get
        Set(ByVal value As clsTrOpcje)
            mvarOpcja = value
        End Set
    End Property

    Public Property Rodzic() As Collection(Of clsTrRodzic)
        Get
            Return mvarRodzic
        End Get
        Set(ByVal value As Collection(Of clsTrRodzic))
            mvarRodzic = value
        End Set
    End Property

    Public Property RodzicDel() As Collection(Of clsTrRodzic)
        Get
            Return mvarRodzicDel
        End Get
        Set(ByVal value As Collection(Of clsTrRodzic))
            mvarRodzicDel = value
        End Set
    End Property

    Public Property Kontrola() As clsInhTrKontrola
        Get
            Return mvarKontrola
        End Get
        Set(ByVal value As clsInhTrKontrola)
            mvarKontrola = value
        End Set
    End Property

    Public Property KontrolaDel() As clsInhTrKontrola
        Get
            Return mvarKontrolaDel
        End Get
        Set(ByVal value As clsInhTrKontrola)
            mvarKontrolaDel = value
        End Set
    End Property

    Public Property PozycjeSchematu() As clsInhTrPozycjeSchematu
        Get
            Return mvarPozycjaSchematu
        End Get
        Set(ByVal value As clsInhTrPozycjeSchematu)
            mvarPozycjaSchematu = value
        End Set
    End Property

#End Region

#Region "Methods"

    Public Sub New()
        MyBase.New()
        mvarPracownik = New clsPracownik()
        mvarPakowanie = New clsPakowanie()
        mvarMaszyna = New clsMaszyna()
        mvarOpcja = New clsTrOpcje()
        mvarRodzic = New Collection(Of clsTrRodzic)
        mvarRodzicDel = New Collection(Of clsTrRodzic)
        mvarKontrola = New clsInhTrKontrola
        mvarKontrolaDel = New clsInhTrKontrola
        mvarPozycjaSchematu = New clsInhTrPozycjeSchematu
    End Sub

    Public Sub New(ByVal ID As Integer, ByVal lvarDbHolder As clsDataBase)

        MyBase.New(ID, lvarDbHolder, VAR_ERROR_HANDLER)
        mvarId = ID

        mvarPracownik = New clsPracownik(MyBase.PracownikId.ToString(), lvarDbHolder, VAR_ERROR_HANDLER)
        mvarPakowanie = New clsPakowanie(MyBase.PakowanieId.ToString(), lvarDbHolder, VAR_ERROR_HANDLER)
        mvarMaszyna = New clsMaszyna(MyBase.MaszynaId, lvarDbHolder, VAR_ERROR_HANDLER)
        mvarOpcja = New clsTrOpcje(MyBase.OpcjaId, lvarDbHolder, VAR_ERROR_HANDLER)

        '
        mvarRodzic = New Collection(Of clsTrRodzic)
        mvarRodzicDel = New Collection(Of clsTrRodzic)

        Dim lvarTabel As DataTable = PobierzR(lvarDbHolder, ID)
        Dim lvarI As Integer = 0

        If Not lvarTabel Is Nothing Then
            For Each lvarRow As DataRow In lvarTabel.Rows
                mvarRodzic.Add(New clsTrRodzic(CStr(lvarRow.Item("id")), lvarDbHolder, VAR_ERROR_HANDLER))
                mvarWaga = mvarWaga + CDbl(mvarRodzic.Item(lvarI).Waga)
                lvarI = lvarI + 1
            Next
        End If

        'KONTROLA
        mvarKontrolaDel = New clsInhTrKontrola()
        mvarKontrola = New clsInhTrKontrola(PobierzKontrole(lvarDbHolder, ID), lvarDbHolder)

        'POZYCJE SCHEMATU
        mvarPozycjaSchematu = New clsInhTrPozycjeSchematu(MyBase.PozycjeSchematuProdukcjiId, lvarDbHolder)
    End Sub

    Public Overloads Sub Save(ByVal lvarDbHolder As clsDataBase)
        MyBase.Save(lvarDbHolder, VAR_ERROR_HANDLER)

        For Each lvarTmp As clsTrRodzic In mvarRodzicDel
            lvarTmp.Delete(lvarDbHolder, VAR_ERROR_HANDLER)
        Next

        For Each lvarTmp As clsTrRodzic In mvarRodzic
            lvarTmp.DrutId = MyBase.Id
            lvarTmp.Save(lvarDbHolder, VAR_ERROR_HANDLER)
            mvarWaga = mvarWaga + CDbl(Replace(lvarTmp.Waga, ".", ","))
        Next

    End Sub

    Public Overloads Sub Delete(ByVal lvarDbHolder As clsDataBase)

        For Each lvarTmp As clsTrRodzic In mvarRodzic
            lvarTmp.Delete(lvarDbHolder, VAR_ERROR_HANDLER)
        Next
        MyBase.Delete(lvarDbHolder, VAR_ERROR_HANDLER)
    End Sub

    'Public Overrides Function ToString() As String
    '    Return MyBase.Id & " - waga: " & mvarWaga
    'End Function

    Shared Function PobierzR(ByVal lvarDbHolder As clsDataBase, Id As Integer) As DataTable

        Dim lvarReader As OdbcDataReader = Nothing
        Dim lvarReturn As DataTable

        If lvarDbHolder.IsOpen Then
            Dim lvarSqlRunner As New clsSqlRunner(lvarDbHolder, "queries_application_tracking", "cls_drut", 1, VAR_ERROR_HANDLER)

            With lvarSqlRunner
                .AddParameter("p_zapytanie", CStr(Id))
                lvarReader = .ExecuteQuery
            End With

            If lvarReader.HasRows Then
                lvarReturn = New DataTable
                lvarReturn.Load(lvarReader)
            End If

        End If

        Return lvarReturn

    End Function

    Shared Function PobierzIloscDrutu(ByVal lvarDbHolder As clsDataBase, Id As Integer) As Double

        Dim lvarReader As OdbcDataReader = Nothing
        Dim lvarReturn As Double

        If lvarDbHolder.IsOpen Then
            Dim lvarSqlRunner As New clsSqlRunner(lvarDbHolder, "queries_application_tracking", "cls_drut", 2, VAR_ERROR_HANDLER)

            With lvarSqlRunner
                .AddParameter("p_zapytanie", CStr(Id))
                lvarReader = .ExecuteQuery
            End With

            If lvarReader.HasRows Then
                lvarReturn = CDbl(IIf(IsDBNull(lvarReader.Item("suma")), Nothing, lvarReader.Item("suma")))
            End If

        End If

        Return lvarReturn

    End Function

    'Sprawdzenie czy podana operacja jest juz wykonywana, czyli czy jest juz drut z danej pozycja
    Shared Function SprawdzeniePozycji(lvarDbHolder As clsDataBase, IdPozycjiSchematu As Integer) As Boolean

        Dim lvarReader As OdbcDataReader = Nothing
        Dim lvarReturn As Boolean

        If lvarDbHolder.IsOpen Then
            Dim lvarSqlRunner As New clsSqlRunner(lvarDbHolder, "queries_application_tracking", "cls_drut", 3, VAR_ERROR_HANDLER)

            With lvarSqlRunner
                .AddParameter("p_pozycji_schematu_id", CStr(IdPozycjiSchematu))
                lvarReader = .ExecuteQuery
            End With

            If lvarReader.HasRows Then
                lvarReturn = True
            End If

        End If

        Return lvarReturn

    End Function

    Shared Function PobierzIdPozycjiZlecenia(lvarDbHolder As clsDataBase, IdDrutu As Integer) As Integer

        Dim lvarReader As OdbcDataReader = Nothing
        Dim lvarReturn As Integer

        If lvarDbHolder.IsOpen Then
            Dim lvarSqlRunner As New clsSqlRunner(lvarDbHolder, "queries_application_tracking", "cls_drut", 4, VAR_ERROR_HANDLER)

            With lvarSqlRunner
                .AddParameter("p_drut_id", CStr(IdDrutu))
                lvarReader = .ExecuteQuery
            End With

            If lvarReader.HasRows Then
                lvarReturn = CInt(IIf(IsDBNull(lvarReader.Item("pozycje_zlecenia_id")), Nothing, lvarReader.Item("pozycje_zlecenia_id")))
            End If

        End If

        Return lvarReturn

    End Function

    Shared Function PobierzKontrole(lvarDbHolder As clsDataBase, Id As Integer) As Integer

        Dim lvarReader As OdbcDataReader = Nothing
        Dim lvarReturn As Integer

        If lvarDbHolder.IsOpen Then
            Dim lvarSqlRunner As New clsSqlRunner(lvarDbHolder, "queries_application_tracking", "cls_drut", 5, VAR_ERROR_HANDLER)

            With lvarSqlRunner
                .AddParameter("p_zapytanie", CStr(Id))
                lvarReader = .ExecuteQuery
            End With

            If lvarReader.HasRows Then
                lvarReturn = CInt(IIf(IsDBNull(lvarReader.Item("id")), Nothing, lvarReader.Item("id")))
            End If

        End If

        Return lvarReturn

    End Function

    'Sprawdzenie czy podany drut jest juz w tabeli tracking.rodzic w kolumnie drut_rodzic_id
    Shared Function PobierzRodzicId(lvarDbHolder As clsDataBase, DrutId As Integer) As Integer

        Dim lvarReader As OdbcDataReader = Nothing
        Dim lvarReturn As Integer

        If lvarDbHolder.IsOpen Then
            Dim lvarSqlRunner As New clsSqlRunner(lvarDbHolder, "queries_application_tracking", "cls_drut", 6, VAR_ERROR_HANDLER)

            With lvarSqlRunner
                .AddParameter("p_zapytanie", CStr(DrutId))
                lvarReader = .ExecuteQuery
            End With

            If lvarReader.HasRows Then
                lvarReturn = CInt(IIf(IsDBNull(lvarReader.Item("id")), Nothing, lvarReader.Item("id")))
            End If

        End If

        Return lvarReturn

    End Function

#End Region

End Class
